//
//  MainViewController.m
//  EasyToo
//
//  Created by zhengzhe on 13-9-10.
//
//

#import "MainViewController.h"
#import "AdView.h"
#import "HomePageViewController.h"
#import "MapViewController.h"
#import "ShoppingViewController.h"
#import "CouponViewController.h"
#import "HotelViewController.h"
#import "DeviceUtil.h"
#import "PPImageScrollingTableViewCell.h"
#import "Goods.h"
#import "Merchant.h"
#import "GoodsDetailViewController.h"
#import "NetworkAlertView.h"
#import "CouponDoorViewController.h"


#define GOODS_SCROLL_BAR_TAG 0
#define MERCHANT_SCROLL_BAR_TAG 1


@interface MainViewController ()

@end

@implementation MainViewController
{
    NSString *imgurl;
}

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    
    [self.view setBackgroundColor:BackgroundColor];
    
    [self initScrollView];
    
    [self initTopBar];
    [self initBannerView];
    [self initButton];
    
    [[ShoppingManager shareManager] setDelegate:self];
    
    [[ShoppingManager shareManager] getRecommends];
    
//    [self initImages];
//    for (int i = 0; i < [self.images count]; i++) {
//        NSDictionary *cellData = [self.images objectAtIndex:i];
//        PPImageScrollingTableViewCell *customCell = [[PPImageScrollingTableViewCell alloc] initWithFrame:CGRectMake(0, 280 + i*160, [DeviceUtil getScreenWidth], 150)];
//        [customCell setBackgroundColor:[UIColor clearColor]];
//        [customCell setDelegate:self];
//        [customCell setImageData:cellData];
//        [customCell setCategoryLabelText:[cellData objectForKey:@"category"] withColor:[UIColor darkGrayColor]];
//        [customCell setTag:i];
//        [customCell setImageTitleTextColor:[UIColor whiteColor] withBackgroundColor:[UIColor colorWithRed:0 green:0 blue:0 alpha:0.7]];
//        [customCell setImageTitleLabelWitdh:90 withHeight:20];
//        [customCell setCollectionViewBackgroundColor:[UIColor clearColor]];
//        [_scrollView addSubview:customCell];
//    }
}

- (void)dealloc
{
    [[ShoppingManager shareManager] setDelegate:nil];
    RelaseAndNil(_goodsArray);
    RelaseAndNil(_merchantArray);
    [super dealloc];
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
}

- (void)initTopBar
{
    [self.navigationItem setTitle:@"一指通科技"];
}

- (void)initScrollView
{
    _scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height)];
    
    _scrollView.contentSize = CGSizeMake(_scrollView.frame.size.width, 700);
    
    [self.view addSubview:_scrollView];
}

- (void)initBannerView
{
    /* -> 添加广告框 */
    AdView *view_Ad = [[AdView alloc] initWithFrame:CGRectMake(0, 0, [DeviceUtil getScreenWidth], 160)];
    [view_Ad setBackgroundColor:[UIColor blackColor]];
//    [view_Ad setCenter:CGPointMake(self.view.bounds.size.width/2.f, 44.f+view_Ad.frame.size.height/2.f)];
    [_scrollView addSubview:view_Ad];
    [view_Ad release];
}

- (void)initButton
{
    UIButton *homePageButton = [[[UIButton alloc] initWithFrame:CGRectMake(15, 180, 60, 60)] autorelease];;
    [homePageButton setBackgroundImage:[UIImage imageNamed:@"便民服务"] forState:UIControlStateNormal];
    [homePageButton addTarget:self action:@selector(onHomePageBtnClick) forControlEvents:UIControlEventTouchUpInside];
    
    [_scrollView addSubview:homePageButton];
    
    UILabel *homePageLabel = [[UILabel alloc] initWithFrame:CGRectMake(15, 245, 60, 16)];
    homePageLabel.text = @"便民服务";
    homePageLabel.backgroundColor = [UIColor clearColor];
    homePageLabel.font = [UIFont boldSystemFontOfSize:14.0f];
    homePageLabel.textColor = [UIColor grayColor];
    homePageLabel.textAlignment = NSTextAlignmentCenter;
    [_scrollView addSubview:homePageLabel];
    
    UIButton *shopButton = [[[UIButton alloc] initWithFrame:CGRectMake(90, 180, 60, 60)] autorelease];;
    [shopButton setBackgroundImage:[UIImage imageNamed:@"购物商城"] forState:UIControlStateNormal];
    [shopButton addTarget:self action:@selector(onShopBtnClick) forControlEvents:UIControlEventTouchUpInside];
    
    [_scrollView addSubview:shopButton];
    
    UILabel *shopLabel = [[UILabel alloc] initWithFrame:CGRectMake(90, 245, 60, 16)];
    shopLabel.text = @"购物商城";
    shopLabel.backgroundColor = [UIColor clearColor];
    shopLabel.font = [UIFont boldSystemFontOfSize:14.0f];
    shopLabel.textColor = [UIColor grayColor];
    shopLabel.textAlignment = NSTextAlignmentCenter;
    [_scrollView addSubview:shopLabel];
    
    UIButton *couponButton = [[[UIButton alloc] initWithFrame:CGRectMake(90+75, 180, 60, 60)] autorelease];;
    [couponButton setBackgroundImage:[UIImage imageNamed:@"优惠券"] forState:UIControlStateNormal];
    [couponButton addTarget:self action:@selector(onCouponBtnClick) forControlEvents:UIControlEventTouchUpInside];
    [_scrollView addSubview:couponButton];
    
    UILabel *couponLabel = [[UILabel alloc] initWithFrame:CGRectMake(90+75, 245, 60, 16)];
    couponLabel.text = @"优惠券";
    couponLabel.backgroundColor = [UIColor clearColor];
    couponLabel.font = [UIFont boldSystemFontOfSize:14.0f];
    couponLabel.textColor = [UIColor grayColor];
    couponLabel.textAlignment = NSTextAlignmentCenter;
    [_scrollView addSubview:couponLabel];
    
    UIButton *hotelButton = [[[UIButton alloc] initWithFrame:CGRectMake(165+75, 180, 60, 60)] autorelease];;
    [hotelButton setBackgroundImage:[UIImage imageNamed:@"旅游路线"] forState:UIControlStateNormal];
    [hotelButton addTarget:self action:@selector(onHotelBtnClick) forControlEvents:UIControlEventTouchUpInside];
    [_scrollView addSubview:hotelButton];
    
    UILabel *hotelLabel = [[UILabel alloc] initWithFrame:CGRectMake(165+75, 245, 60, 16)];
    hotelLabel.text = @"旅游路线";
    hotelLabel.backgroundColor = [UIColor clearColor];
    hotelLabel.font = [UIFont boldSystemFontOfSize:14.0f];
    hotelLabel.textColor = [UIColor grayColor];
    hotelLabel.textAlignment = NSTextAlignmentCenter;
    [_scrollView addSubview:hotelLabel];
    
}

- (void)onHomePageBtnClick
{
    HomePageViewController* homePageCtrl = [[[HomePageViewController alloc] init] autorelease];
    [self.navigationController pushViewController:homePageCtrl animated:YES];
}


- (void)onShopBtnClick
{
    ShoppingViewController* shopCtrl = [[[ShoppingViewController alloc] init] autorelease];
    [self.navigationController pushViewController:shopCtrl animated:YES];
}

- (void)onCouponBtnClick
{
    CouponViewController* couponCtrl = [[[CouponViewController alloc] init] autorelease];
    [self.navigationController pushViewController:couponCtrl animated:YES];
}

- (void)onHotelBtnClick
{
    HotelViewController* hotelCtrl = [[[HotelViewController alloc] init] autorelease];
    [self.navigationController pushViewController:hotelCtrl animated:YES];
}

#pragma mark - ShoppingManagerDelegate

- (void)onGetRecommendProducts:(BOOL)isSuccess
                    GoodsArray:(NSMutableArray*)goodsArray
                 MerchantArray:(NSMutableArray*)merchantArray
{
    if (!isSuccess || !goodsArray || !merchantArray) {
        return;
    }
    
    _goodsArray = [goodsArray retain];
    _merchantArray = [merchantArray retain];
    
    NSMutableDictionary *goodsDic = [[[NSMutableDictionary alloc] init] autorelease];
    [goodsDic setObject:@"特价优惠" forKey:@"category"];
    
    NSMutableArray *goodsImageArray = [[[NSMutableArray alloc] init] autorelease];

    for (int i = 0; i < [_goodsArray count]; i++) {

        NSMutableDictionary *goodsImageDic = [[[NSMutableDictionary alloc] init] autorelease];
        Goods *goods = [_goodsArray objectAtIndex:i];
        [goodsImageDic setObject:goods.imgUrl forKey:@"netImage"];
        [goodsImageDic setObject:goods.name forKey:@"title"];
        
        [goodsImageArray addObject:goodsImageDic];
    }
    
    [goodsDic setObject:goodsImageArray forKey:@"images"];
    
    [self createGoodsScrollBar:goodsDic];
    
    NSMutableDictionary *merchantDic = [[[NSMutableDictionary alloc] init] autorelease];
    [merchantDic setObject:@"热门商家" forKey:@"category"];
    
    NSMutableArray *merchantImageArray = [[[NSMutableArray alloc] init] autorelease];
    
    for (int i = 0; i < [_merchantArray count]; i++) {
        
        NSMutableDictionary *merchantImageDic = [[[NSMutableDictionary alloc] init] autorelease];
        Merchant *merchant = [_merchantArray objectAtIndex:i];
        [merchantImageDic setObject:merchant.imageUrl forKey:@"netImage"];
        [merchantImageDic setObject:merchant.brand forKey:@"title"];
        
        [merchantImageArray addObject:merchantImageDic];
    }
    
    [merchantDic setObject:merchantImageArray forKey:@"images"];
    
    [self createMerchantScrollBar:merchantDic];
    
}

- (void)createGoodsScrollBar:(NSMutableDictionary*)goodsDic
{
    NSDictionary *cellData = goodsDic;
    PPImageScrollingTableViewCell *customCell = [[PPImageScrollingTableViewCell alloc] initWithFrame:CGRectMake(0, 280, [DeviceUtil getScreenWidth], 120)];
    [customCell setBackgroundColor:[UIColor clearColor]];
    [customCell setDelegate:self];
    [customCell setImageData:cellData];
    [customCell setCategoryLabelText:[cellData objectForKey:@"category"] withColor:[UIColor grayColor]];
    [customCell setTag:GOODS_SCROLL_BAR_TAG];
    [customCell setImageTitleTextColor:[UIColor whiteColor] withBackgroundColor:[UIColor colorWithRed:0 green:0 blue:0 alpha:0.7]];
    [customCell setImageTitleLabelWitdh:90 withHeight:22];
    [customCell setCollectionViewBackgroundColor:[UIColor clearColor]];
    [_scrollView addSubview:customCell];
}

- (void)createMerchantScrollBar:(NSMutableDictionary*)merchantDic
{
    NSDictionary *cellData = merchantDic;
    PPImageScrollingTableViewCell *customCell = [[PPImageScrollingTableViewCell alloc] initWithFrame:CGRectMake(0, 280 + 150, [DeviceUtil getScreenWidth], 120)];
    [customCell setBackgroundColor:[UIColor clearColor]];
    [customCell setDelegate:self];
    [customCell setImageData:cellData];
    [customCell setCategoryLabelText:[cellData objectForKey:@"category"] withColor:[UIColor darkGrayColor]];
    [customCell setTag:MERCHANT_SCROLL_BAR_TAG];
    [customCell setImageTitleTextColor:[UIColor whiteColor] withBackgroundColor:[UIColor colorWithRed:0 green:0 blue:0 alpha:0.7]];
    [customCell setImageTitleLabelWitdh:90 withHeight:22];
    [customCell setCollectionViewBackgroundColor:[UIColor clearColor]];
    [_scrollView addSubview:customCell];
}

#pragma mark - PPImageScrollingTableViewCellDelegate

- (void)scrollingTableViewCell:(PPImageScrollingTableViewCell *)scrollingTableViewCell didSelectImageAtIndexPath:(NSIndexPath*)indexPathOfImage atCategoryRowIndex:(NSInteger)categoryRowIndex
{
    NSLog(@"%d, %d", indexPathOfImage.row, scrollingTableViewCell.tag);
    
    int scrollBarTag = scrollingTableViewCell.tag;
    int index = indexPathOfImage.row;
    
    
    if (scrollBarTag == GOODS_SCROLL_BAR_TAG)
    {
        Goods *goods = [_goodsArray objectAtIndex:index];
        
        GoodsDetailViewController *goodsDetailCtrl = [[GoodsDetailViewController alloc] init];
        goodsDetailCtrl.goodsInfo = goods;
        [self.navigationController pushViewController:goodsDetailCtrl animated:YES];
        [goodsDetailCtrl release];
    }
    else if (scrollBarTag == MERCHANT_SCROLL_BAR_TAG)
    {
        Merchant *merchant = [_merchantArray objectAtIndex:index];
        NSLog(@"");
        
        imgurl = merchant.imageUrl;
        NSLog(@"11111111111111");
        NSLog(imgurl);
        NSLog(merchant.merchantId);
        NSLog(@"222222222222222");
        //请求数据
        CouponManager *couponmanager = [CouponManager shareManager];
        couponmanager.delegate = self;
        [couponmanager getShops:merchant.merchantId];
    }
    
    
    
}

#pragma network to shoplist
- (void)getShopsFinish:(BOOL)isSuccess
             shopArray:(NSMutableArray*)shopArray
{
    //取消等待
    NetworkAlertView *networking = [NetworkAlertView shareView];
    [networking delView];
    
    CouponDoorViewController *detailController = [[CouponDoorViewController alloc] init];
    detailController.shopInfos = shopArray;
    detailController.imgurl = imgurl;
    [self.navigationController pushViewController:detailController animated:YES];
    [detailController release];
}

@end
